#include "struct.h"
#include "Menu.h"
#include <SDL/SDL_getenv.h>

int main ( int argc, char** argv )
{
    int pid;
    pSeg=(ST_IHM*)InitSegmentMemoire("fic", '1', sizeof(ST_IHM));
    printf("TAille : sizeof(ST_IHM) = %ld\n",sizeof(ST_IHM));
    if((int*)pSeg == (int*)-1)
    {
        printf("ERREUR DE SEGMENT MEMOIRE PARTAGE\n");
    }
    memset(pSeg,0,sizeof(ST_IHM));
    char *const* Inutile=NULL;

    pid = fork();
    if(pid == 0)
    {
        execv("./main",Inutile);
        printf("Erreur d'ouverture de l'executable\n");
        exit(0);

    }

    if(pSeg == 0)
    {
        printf("Erreur de Segment Memoire\n");
        exit(0);
    }

    // initialize SDL video
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return 1;
    }

    if(TTF_Init() < 0 )
    {
        printf("Impossible d'initialiser SDL_ttf : %s", TTF_GetError());
        return 1;
    }

    // make sure SDL cleans up before exit
    atexit(SDL_Quit);

    putenv("SDL_VIDEO_WINDOW_POS=center"); //pour centrer la fenêtre

    Config_Menu();
    MajScreen ();
    // program main loop
    while (1)
    {
        // message processing loop

        SDL_Event event;
        while (SDL_PollEvent(&event))
        {

            // check for messages
            switch (event.type)
            {
                // exit if the window is closed
            case SDL_QUIT:
                Quitter();
                break;

                // check for keypresses
            case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        Quitter();
                    break;
                }
            case SDL_MOUSEBUTTONUP:

                if ((event.button.y > rect1.y) && (event.button.y <= (rect1.y +Bouton1->h )) && (event.button.x > rect1.x) && ((event.button.x <= (rect1.x + Bouton1->w))))// si clic sur boutton
                {
                    printf("NOUVELLE PARTIE\n");
                    pSeg->menu_action = 1;
                    New_Game();
                }

                if ((event.button.y > rect2.y) && (event.button.y <= (rect2.y +Bouton2->h )) && (event.button.x > rect2.x) && ((event.button.x <= (rect2.x + Bouton2->w))))// si clic sur boutton
                {
                    pSeg->menu_action = 2;
                    printf("CONFIG\n");
                }

                if ((event.button.y > rect3.y) && (event.button.y <= (rect3.y +Bouton3->h )) && (event.button.x > rect3.x) && ((event.button.x <= (rect3.x + Bouton3->w))))// si clic sur boutton
                {
                    printf("REGLES DU JEU\n");
                    AfficheRegles();
                    Config_Menu();
                    MajScreen ();
                    pSeg->menu_action = 0;
                }
                if ((event.button.y > rect4.y) && (event.button.y <= (rect4.y +Bouton4->h )) && (event.button.x > rect4.x) && ((event.button.x <= (rect4.x + Bouton4->w))))// si clic sur boutton
                {
                    pSeg->menu_action = 3;
                    Quitter();
                }
            break;

            default:
                break;
            } // end switch
        } // end of message processing

    } // end main loop
    return 0;
}

